package com.yaohe.consumer.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import com.yaohe.consumer.network.FTPTransfer;
import com.yaohe.consumer.network.FtpConfigure;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class AsyncImageLoader {

	 private HashMap<String, SoftReference<Drawable>> imageCache;

	     public AsyncImageLoader() {
	    	 imageCache = new HashMap<String, SoftReference<Drawable>>();
	     }
	  
	     public Drawable loadDrawable(final String imageUrl, final int position, final ImageCallback imageCallback) {
	         if (imageCache.containsKey(imageUrl)) {
	             SoftReference<Drawable> softReference = imageCache.get(imageUrl);
	             Drawable drawable = softReference.get();
	             
	             if (drawable != null) {
	                 return drawable;
	             }
	         }
	         final Handler handler = new Handler() {
	             public void handleMessage(Message message) {
	                 imageCallback.imageLoaded((Drawable) message.obj, imageUrl, position);
	             }
	         };
	         new Thread() {
	             @Override
	             public void run() {
	                 //Drawable drawable = loadImageFromUrl(imageUrl);
	            	 Drawable drawable = loadImageByFtp(imageUrl);
	                 imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
	                 Message message = handler.obtainMessage(0, drawable);
	                 handler.sendMessage(message);
	             }
	         }.start();
	         return null;
	     }
	  
	    //change the function to access ftp
		public static Drawable loadImageFromUrl(String url) {
			URL m;
			InputStream i = null;
			try {
				m = new URL(url);
				i = (InputStream) m.getContent();
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Drawable d = Drawable.createFromStream(i, "src");
			return d;
		}
		
		public static Drawable loadImageByFtp(String name){
			
			DownPic parseRes = parseShopLogoOrInfoPic(name);
			
			//Log.d("YaoheService","Count:" +(++count));
			ByteArrayOutputStream output = new ByteArrayOutputStream(); 
			FTPTransfer.downloadPic(parseRes.getName(),parseRes.getType(), output);
			
			byte[] buff = output.toByteArray();
			ByteArrayInputStream bin = new ByteArrayInputStream(buff);
			
			Drawable d = Drawable.createFromStream(bin, "src");
			
			return d;
		}
		
	     public interface ImageCallback {
	         public void imageLoaded(Drawable imageDrawable, String imageUrl, int index);
	     }
	     
	     public static DownPic parseShopLogoOrInfoPic(String name){
	    	 String[]array = name.split("_");
	    	 DownPic res = new DownPic(array[1],Integer.parseInt(array[0]));
	    	 return res;
	     }
	     
	     public static class DownPic{
	    	 String name;
	    	 int type;
	    	 
	    	 public DownPic(String name, int type){
	    		 this.name = name;
	    		 this.type = type;
	    	 }
	    	 public String getName(){
	    		 return name;
	    	 }
	    	 public int getType(){
	    		 return type;
	    	 }
	     }

}
